<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Cart;
class CartController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
        //拿数据库数据
        $carts = (Cart::content());
        // dd($carts);
        $count = Cart::count();
        //商品总价
        $pay = Cart::subtotal();
        return view('home.cart_index')->with('cats',$carts)->with('count',$count)->with('pay',$pay);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $good = DB::table('goods')->where('goods_id',$id)->first();
        $res=Cart::add($good->goods_id,$good->goods_name,1,$good->shop_price,['goods_price'=>$good->goods_price]);
        // dd($res);
        return redirect('cart');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function addCart($goods_id,$num=1)
    {
        $good = DB::table('goods')->where('goods_id',$goods_id)->first();
        Cart::add($good->goods_id,$good->goods_name,$num,$good->shop_price,['goods_price'=>$good->goods_price]);
        return redirect('cart');
    }

    //移出购物车
    public function removeCart($rowid)
    {   
        // dd($rowid);
       Cart::remove($rowid);
       return redirect('cart'); 
    }

    //清空购物车
    public function dellCart()
    {
        Cart::destroy();
        return redirect('/cat');
    }
}
